package com.hawk.eye.server.controller.hawkeye;

import com.hawk.eye.common.core.ApiResult;
import com.hawk.eye.framework.utils.CollectionUtil;
import com.hawk.eye.common.page.PageQuery;
import com.hawk.eye.common.page.PageResult;
import com.hawk.eye.server.convert.ServiceConfigConvert;
import com.hawk.eye.server.domain.dto.resp.ServiceConfigRespDTO;
import com.hawk.eye.server.domain.vo.req.ServiceConfigReqVO;
import com.hawk.eye.server.domain.vo.resp.ServiceConfigDetailRespVO;
import com.hawk.eye.server.domain.vo.resp.ServiceConfigRespVO;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.hawk.eye.server.service.IServiceConfigService;

/**
 * 告警配置 Controller
 *
 * @author salad
 * @date 2023-09-23
 */
@RestController
@RequestMapping("/hawk-eye/service")
@RequiredArgsConstructor
public class ServiceConfigController {

    private final IServiceConfigService serviceConfigService;

    /**
     * 分页查询告警配置
     * @param pageQuery
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:service-config:query')")
    @GetMapping("/page")
    public PageResult<ServiceConfigRespVO> page(PageQuery pageQuery, ServiceConfigReqVO reqVO) {
        PageResult<ServiceConfigRespDTO> pageResult = serviceConfigService.page(pageQuery, ServiceConfigConvert.INSTANCE.reqVoToReqDto(reqVO));
        return PageResult.of(pageResult, ServiceConfigConvert.INSTANCE::respDtoToRespVo);
    }

    /**
     * 新增告警配置
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:service-config:add')")
    @PostMapping("/save")
    public ApiResult save(@RequestBody @Validated ServiceConfigReqVO reqVO) {
        boolean status = serviceConfigService.save(ServiceConfigConvert.INSTANCE.reqVoToReqDto(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 修改告警配置
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:service-config:edit')")
    @PutMapping("/update")
    public ApiResult update(@RequestBody @Validated ServiceConfigReqVO reqVO) {
        boolean status = serviceConfigService.update(ServiceConfigConvert.INSTANCE.reqVoToReqDto(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 告警配置详情
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:service-config:query')")
    @GetMapping(value = "detail/{id}")
    public ApiResult<ServiceConfigDetailRespVO> detail(@PathVariable("id") Long id) {
        return ApiResult.ok(ServiceConfigConvert.INSTANCE.to(serviceConfigService.detail(id)));
    }

    /**
     * 删除告警配置
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:service-config:remove')")
    @DeleteMapping("/remove/{ids}")
    public ApiResult remove(@PathVariable Long[] ids) {
        return ApiResult.status(serviceConfigService.removeBatchByIds(CollectionUtil.toList(ids)));
    }

    /**
     * 修改告警配置状态
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:service-config:edit')")
    @PutMapping("/change-status")
    public ApiResult changeStatus(@RequestBody ServiceConfigReqVO reqVO) {
        boolean status = serviceConfigService.changeStatus(ServiceConfigConvert.INSTANCE.reqVoToReqDto(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 服务名列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:service-config:query')")
    @GetMapping(value = "name-list")
    public ApiResult nameList() {
        return ApiResult.ok(serviceConfigService.nameList());
    }

    /**
     * 用户列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:service-config:query')")
    @GetMapping("/user-list")
    public ApiResult userList() {
        return ApiResult.ok(serviceConfigService.userList());
    }

    /**
     * 用户列表，不包含自己
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:service-config:query')")
    @GetMapping("/user-list-not-self")
    public ApiResult userListNotSelf() {
        return ApiResult.ok(serviceConfigService.userListNotSelf());
    }

}
